
import {useEffect, useState} from "react";
import {Form, Select} from "antd"

export const roleList = [
    "店长","茶艺师","保洁"
].map(item=>({label:item,value:item}))
export const deviceTypeList = [
    "大门","房间门","电源"
].map((item,key)=>({label:item,value:key+1}))
export const deviceActionList = [
    "开大门","开房间门","接通电源"
].map((item,key)=>({label:item,value:item}))
export const deviceProductList = [
    "Aqara","通通锁","智能喇叭"
].map((item,key)=>({label:item,value:item}))

// 人员状态
export const operatorStatusList = ["空闲中","工作中"].map((item,key)=>({label:item,value:key+1}))
//订单状态 // 订单状态 1 待付款 2 支付成功 3 消费中 4 已完成 5 已取消
export const orderStatusList = ["待付款","支付成功","消费中","已完成","自动取消","已取消"].map((item,key)=>({label:item,value:key+1}))

// 会员状态
export const memberStatusList = ["正常","停用"].map((item,key)=>({label:item,value:key}))
//房间状态
export const roomStatusList = ["空闲中","空闲中", "使用中" , "维护中" , "待打扫" ,"保洁中"].map((item,key)=>({label:item,value:key}))
//获取加盟商列表
export function useCms({companyId=0}){
    const [list,setList] = useState([])

    useEffect(()=>{
        ax.get("/api/getCustomerList",{
            params:{
                current:1,
                pageSize:999,
                companyId
            }
        }).then(res=>{
            setList(res.data)
        })
    },[])
    const filterOption = (input, option) =>
        (option?.label ?? '').toLowerCase().includes(input.toLowerCase());
    return {
        cmsList:list.map(item=>({label:item.name,value:item.ID})),
        CmsFormItem:<Form.Item
            name={"companyId"}
            label={"加盟商"}
            rules={[
                {
                    required: true,
                    message: '请选择!',
                },
            ]}
        >
            <Select
                options={list.map(item=>({label:item.name,value:item.ID}))}
                placeholder={"请选择"}
                allowClear
                showSearch
                filterOption={filterOption}
            />
        </Form.Item>
    }
}
export function useRooms({companyId=0,formProps={}}){
    const [roomList,setRoomList] = useState([]),
        [original,setData] = useState([]);

    useEffect(()=>{
        if (companyId === 0){
            return
        }
        ax.get("/api/roomList",{
            params:{
                current:1,
                pageSize:999,
                company_id:companyId,
            }
        }).then(res=>{
            setData(res.data);
            setRoomList(res.data.map(item=>({
                label:item.name,
                value:item.ID
            })))
        })
    },[companyId])
    const filterOption = (input, option) =>
        (option?.label ?? '').toLowerCase().includes(input.toLowerCase());

    return {
        original,
        roomList,
        RoomFormItem:<Form.Item
            name={"roomId"}
            label={"房间"}
            {...formProps}
        >
            <Select
                options={roomList}
                placeholder={"请选择"}
                allowClear
                showSearch
                filterOption={filterOption}
                mode={"multiple"}
            />
        </Form.Item>
    }
}
export function useDevices({roomId=0,formProps={}}){
    const [deviceList,setDeviceList] = useState([])

    useEffect(()=>{
        if (roomId === 0){
            return
        }
        ax.get("/api/getDeviceList",{
            params:{
                current:1,
                pageSize:999,
                roomId:roomId,
            }
        }).then(res=>{
            const list = res.data.map(item=>({
                label:item.name,
                value:item.ID
            }))
            setDeviceList(list)
        })
    },[roomId])
    const filterOption = (input, option) =>
        (option?.label ?? '').toLowerCase().includes(input.toLowerCase());

    return {
        deviceList,
        DeviceFormItem:<Form.Item
            name={"deviceId"}
            label={"选择设备"}
            {...formProps}
        >
            <Select
                options={deviceList}
                placeholder={"请选择"}
                allowClear
                showSearch
                filterOption={filterOption}
            />
        </Form.Item>
    }
}

export function useBankInfo(){
    const [bankInfo,setbankInfo] = useState([])
    async function queryBankInfo(params={}){
        return ax.get("/api/getBankInfo",{
            params:{
                current:1,
                pageSize:999,
                ...params
            }
        })
    }
    useEffect(()=>{
        queryBankInfo().then(res=>{
            setbankInfo(res.data)
        })
    },[])
    const filterOption = (input, option) =>
        (option?.label ?? '').toLowerCase().includes(input.toLowerCase());

    return {
        queryBankInfo,
        bankInfo,
    }
}